package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.field.RelyPoJo;
import com.xbongbong.paas.field.SubRelyFieldPojo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.domain.dao.LinkProductRelyDao;
import com.xbongbong.pro.domain.entity.LinkProductRelyEntity;
import com.xbongbong.saas.model.LinkProductRelyModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("linkProductRelyModelImpl")
public class LinkProductRelyModelImpl implements LinkProductRelyModel {
    @Resource

    private LinkProductRelyDao linkProductRelyDao;
    @Override
    public Integer insert(LinkProductRelyEntity entity) throws XbbException {
        return linkProductRelyDao.insert(entity);
    }

    @Override
    public Integer insertBatch(List<LinkProductRelyEntity> linkProductRelyEntityList) {
        return linkProductRelyDao.insertBatch(linkProductRelyEntityList);
    }

    @Override
    public Integer updateBatch(List<LinkProductRelyEntity> linkProductRelyEntityList) {
        return linkProductRelyDao.updateBatch(linkProductRelyEntityList);
    }

    @Override
    public Integer update(LinkProductRelyEntity entity) throws XbbException {
        return linkProductRelyDao.update(entity);
    }

    @Override
    public List<LinkProductRelyEntity> findEntitys(Map<String, Object> param) {
        return linkProductRelyDao.findEntitys(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return null;
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public LinkProductRelyEntity getByAttrAndFormIdAndLinkFormId(String corpid, String attr, Long formId, Long linkFormId, Integer del) {
        return linkProductRelyDao.getByAttrAndFormIdAndLinkFormId(corpid, attr, formId, linkFormId, del);
    }

    @Override
    public LinkProductRelyEntity getByAttrAndFormIdAndSourceBusinessType(String corpid, String attr, Long formId, Integer sourceBusinessType, Integer del) {
        return linkProductRelyDao.getByAttrAndFormIdAndSourceBusinessType(corpid, attr, formId, sourceBusinessType, del);
    }

    @Override
    public List<LinkProductRelyEntity> getByFormIdAndLinkFormId(String corpid, Long formId, Long linkFormId, Integer del) {
        return linkProductRelyDao.getByFormIdAndLinkFormId(corpid, formId, linkFormId, del);
    }

    @Override
    public List<LinkProductRelyEntity> getByFormId(String corpid, Long formId, Integer del) {
        return linkProductRelyDao.getByFormId(corpid, formId, del);
    }

    @Override
    public List<RelyPoJo> selectProductRely(String corpid, Long formId) {
        List<RelyPoJo> relyPoJoList = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);

        List<LinkProductRelyEntity> linkProductRelyEntities = getByFormId(corpid, formId, DelEnum.NORMAL.getDel());
        for (LinkProductRelyEntity item : linkProductRelyEntities) {
            RelyPoJo relyPoJo = new RelyPoJo();
            relyPoJo.setDisplayField(item.getDisplayField());
            relyPoJo.setLinkField(item.getLinkField());
            relyPoJo.setLinkFieldType(item.getLinkFieldType());
            relyPoJo.setTargetField(item.getTargetField());
            relyPoJo.setTargetFieldType(item.getTargetFieldType());
            relyPoJo.setSourceBusinessType(item.getSourceBusinessType());
            relyPoJo.setSourceAppId(item.getSourceAppId());
            relyPoJo.setSourceMenuId(item.getSourceMenuId());
            relyPoJo.setSourceFormId(item.getSourceFormId());
            relyPoJo.setSourceSaasMark(item.getSourceSaasMark());
            relyPoJo.setSubRelyFieldList(item.getSubRelyFieldList().toJavaList(SubRelyFieldPojo.class));
            relyPoJoList.add(relyPoJo);
        }

        return relyPoJoList;
    }
}
